package com.jdbc.homework2;

import com.jdbc.homework.DataSourceUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * ClassName: PetManager
 * Passage: com.jdbc.homework2
 * Description:
 *
 * @Author 余大帅逼
 * @Create 2025/2/26 19:59
 * @Version 1.0
 **/
public class PetManager {

    public static void addPet(String nickname, String type, int age, String gender) {
        String sql = "INSERT INTO pets (nickname, type, age, gender) VALUES (?, ?, ?, ?)";
        try (Connection conn = DataSourceUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, nickname);
            ps.setString(2, type);
            ps.setInt(3, age);
            ps.setString(4, gender);
            ps.executeUpdate();
            System.out.println("宠物添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updatePet(int petId, String nickname, String type, int age, String gender) {
        String sql = "UPDATE pets SET nickname = ?, type = ?, age = ?, gender = ? WHERE id = ?";
        try (Connection conn = DataSourceUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, nickname);
            ps.setString(2, type);
            ps.setInt(3, age);
            ps.setString(4, gender);
            ps.setInt(5, petId);
            ps.executeUpdate();
            System.out.println("宠物信息更新成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deletePet(int petId) {
        String sql = "DELETE FROM pets WHERE id = ?";
        try (Connection conn = DataSourceUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, petId);
            ps.executeUpdate();
            System.out.println("宠物删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void viewPets(String sortBy) {
        String sql = "SELECT * FROM pets";
        if ("age".equals(sortBy)) {
            sql += " ORDER BY age";
        } else if ("price".equals(sortBy)) {
            sql += " ORDER BY price";
        } else {
            sql += " ORDER BY id"; // 默认按ID排序
        }

        try (Connection conn = DataSourceUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                System.out.println("ID: " + rs.getInt("id") + ", Nickname: " + rs.getString("nickname")
                        + ", Type: " + rs.getString("type") + ", Age: " + rs.getInt("age"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void buyPet(int petId, int userId) {
        String checkPointsSql = "SELECT points FROM users WHERE id = ?";
        String updatePetSql = "UPDATE pets SET owner_id = ? WHERE id = ?";
        String updateUserSql = "UPDATE users SET points = points - 10 WHERE id = ?";  // 假设扣除10积分

        try (Connection conn = DataSourceUtils.getConnection();
             PreparedStatement ps1 = conn.prepareStatement(checkPointsSql);
             PreparedStatement ps2 = conn.prepareStatement(updatePetSql);
             PreparedStatement ps3 = conn.prepareStatement(updateUserSql)) {

            // 检查用户积分
            ps1.setInt(1, userId);
            try (ResultSet rs = ps1.executeQuery()) {
                if (rs.next()) {
                    int points = rs.getInt("points");
                    if (points < 10) {
                        System.out.println("积分不足，无法购买宠物！");
                        return;
                    }
                }
            }

            // 设置宠物主人
            ps2.setInt(1, userId);
            ps2.setInt(2, petId);
            ps2.executeUpdate();

            // 扣除顾客积分
            ps3.setInt(1, userId);
            ps3.executeUpdate();

            System.out.println("购买成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

